<?php
namespace app\index\controller;

use app\common\model\Draw as DrawModel;
use app\common\model\Recharge as RechargeModel;
use app\common\model\Manager as ManagerModel;

/**
 * 提现明细控制器
 * Class Index
 * @package app\index\controller
 */
class Draw extends Base
{
    /**
     * [index  提现明细列表]
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new DrawModel;
        try{
            if(!empty($param['con'])){
                $map['m.tel'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['d.addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['d.comid'] = $user['comid'];
            $count = $model->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getLists($map, $this->from, $this->size);
            foreach($lists as &$v){
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看提现列表');
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getCandraw  获取可提现金额]
     * @return json
     * @author [C。]
     */
    public function getCandraw()
    {
        $user = $this->user;
        $model = new RechargeModel;
        $dmodel = new DrawModel;
        try{
            $total_amount = $model->getSum(['states'=>1,'comid'=>$user['comid']],'amt');
            $has_draw = $dmodel->getSum(['comid'=>$user['comid'],'states'=>['neq',2]],'drawamt');
            $ok_draw = $total_amount - $has_draw;
            $data = array(
                'total_amount' => strval($total_amount),
                'has_draw' => strval($has_draw),
                'ok_draw' => strval($ok_draw)
            );
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [getDrawsub  提现内容]
     * @param
     * @return json
     * @author [C。]
     */
    public function postTodraw()
    {
        $param = input('param.');
        $model = new RechargeModel;
        $dmodel = new DrawModel;
        $mmodel = new ManagerModel;
        $user = $this->user;

        try{
            $total_amount = $model->getSum(['states'=>1,'comid'=>$user['comid']],'amt');
            $has_draw = $dmodel->getSum(['comid'=>$user['comid'],'states'=>['neq',2]],'drawamt');
            $ok_draw = $total_amount - $has_draw;
            if($param['amt'] > $ok_draw){
                return admin_json(-1, '提现金额超出可提现范围！');
            }
            $manager = $mmodel->findData($user['mid'],'id','tel');
            $maxnum = $dmodel->getMaxnum(['comid'=>$user['comid']],'num');
            $darr = array();
            $darr['manager_id'] = $user['mid'];
            $darr['comid'] = $user['comid'];
            $darr['bef_amt'] = $ok_draw;
            $darr['af_amt'] = $ok_draw - $param['amt'];
            $darr['num'] = 1 + $maxnum;
            $darr['drawamt'] = $param['amt'];
            $darr['ymd'] = date('Ymd');
            $darr['addtime'] = time();
            $darr['states'] = 0;
            $darr['paytime'] = 0;
            $darr['payrmk'] = '';
            $darr['payimg'] = '';
            $darr['tel'] = $manager['tel'];
            $darr['orderno'] = $darr['ymd'] . time() . rand(1111, 9999);
            $dmodel->addData($darr);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, '提交失败'.$e->getMessage());
        }
        return admin_json(1, '提交成功');
    }
}